#ifndef	EF_CONNECTION_H
#define	EF_CONNECTION_H

#include "ef_common.h"
#include "ef_sock.h"
#include "ef_timer.h"
#include <string>
#include <list>

namespace	ef{

class	net_thread;
class	message;

class	connection{
	public:

		connection(SOCKET fd = INVALID_SOCKET, uint32 id = 0);

		virtual	~connection();

		int32	set_notify(int32 noti);

		int32	get_notify(){

			return	m_noti;

		}

		int32	clear();

		int32	start_timer(int32 id, int32 timeout);

		int32	stop_time(int32 id);

		virtual	int32	handle_read();

		virtual	int32	handle_write();

		virtual	int32	handle_timer(int32 id);

		virtual	int32	recycle();

		virtual	int32	on_create();

		void	set_fd(SOCKET fd){
			
			m_fd = fd;

		}

		SOCKET	get_fd() const{

			return	m_fd;

		}

		void	set_id(uint32 id){

			m_id = id;

		}

		uint32	get_id() const{

			return	m_id;

		}
		
		int32	set_thread(net_thread *thr);

		net_thread*	get_thread(){

			return	m_thread;

		}

		int32	set_addr(struct in_addr addr, int32 port){

			m_addr	=	addr;

			m_port	=	port;

			return	0;
		}

		int32	get_addr(struct in_addr &addr, int32 &port) const{

			addr	=	m_addr;

			port	=	m_port;

			return	0;
		}

		int32	send_message(const std::string &msg);
		
	private:

		int32	find_del_timer(int32 id, timer &tm);

		int32	do_send();

		int32	send_(const char* buf, uint32 len);

		const static char *tag;
		
		SOCKET		m_fd;

		uint32		m_id;

		net_thread	*m_thread;

		int32		m_noti;

		struct in_addr	m_addr;

		int32	m_port;
		//be::MUTEX	m_cs;

		std::list<timer>	m_timers;

		std::list<std::string>	m_msgs;

		uint32	m_cur_msg_start;

};


};

#endif

